<?php
include_once('inc/functions.inc.php');

if (!isEmpty(getParam('operation'))) {

    switch (getParam('operation')) {
        case 'contenido': {
                fntAcademicoCargosContenido($db);
                break;
            }
        case 'reporte': {
                fntAcademicoCargosReporte($db);
                break;
            }
        case 's': {
                fntAcademicoCargosGrabar($db);
                break;
            }
        case 'cuentas': {
                fntAcademicoCargosContabilidadGetCuentasUltimoNivel($db);
                break;
            }
        case 'del': {
                fntAcademicoCargosDelDetalle($db);
                break;
            }
        case 'delCargo': {
                fntAcademicoCargosDelCargo($db);
                break;
            }	
    }
    die();
}

function fntAcademicoCargosDelCargo($db) {
    $sql = sprintf("delete from cllg_academico_cargos_detail where FK_CARGOS=%s", getParam("idCargo"));
    $db->query($sql);
	$sql = sprintf("delete from cllg_academico_cargos where ID=%s", getParam("idCargo"));
    $db->query($sql);    
}

function fntAcademicoCargosDelDetalle($db) {
    $sql = sprintf("delete from cllg_academico_cargos_detail where ID=%s", getParam("idDetalle"));
    $db->query($sql);
}

function fntAcademicoCargosContenido($db) {
    //VARIBLES
    $strId = "";
    $strQuery = "";
    $arrInfo = array();
    $strCodigo = "";
    $strNombre = "";
    $strSelected = "";
    //VARIBLES

    $strId = isset($_POST["hdnAcademicoCargos_Id"]) ? $_POST["hdnAcademicoCargos_Id"] : "";
    if (!empty($strId)) {
        $strQuery = "SELECT * FROM cllg_academico_cargos WHERE ID = '{$strId}'";
        $qTMP = $db->query($strQuery);
        while ($rTMP = $db->fetch_array($qTMP)) {
            $arrInfo = $rTMP;
        }
        $db->free_result($qTMP);
    }

    $strCodigo = isset($arrInfo["CODIGO"]) ? $arrInfo["CODIGO"] : "";
    $strPeriodo = isset($arrInfo["FK_PERIODO"]) ? $arrInfo["FK_PERIODO"] : "";
    $strGrado = isset($arrInfo["FK_GRADO"]) ? $arrInfo["FK_GRADO"] : "";
    $strActivo = isset($arrInfo["ACTIVO"]) ? $arrInfo["ACTIVO"] : "";
?>
    <script type="text/javascript">
        var intFilaPadres = 0;
    </script>
    <br>
    <br>
    <div style="width: 100px; height: 30px; float: left;"><b>Codigo</b></div>
    <div style="width: 900px; height: 30px; float: left;"><input type="text" name="txtAcademicoCargos_codigo" value="<?php print $strCodigo; ?>"></div>
    <div style="width: 100px; height: 30px; float: left;"><b>Periodo</b></div>
    <div style="width: 900px; height: 30px; float: left;">
        <select name="txtAcademicoCargos_fk-periodo">
            <option value=""></option>
        <?php
        $strQuery = "SELECT * FROM cllg_academico_periodos WHERE ACTIVO = 'Y'";
        $qTMP = $db->query($strQuery);
        while ($rTMP = $db->fetch_array($qTMP)) {
            $strSelected = ( $strPeriodo == $rTMP["ID"] ) ? "selected" : "";
        ?>
            <option value="<?php print $rTMP["ID"]; ?>" <?php print $strSelected; ?>><?php print $rTMP["NOMBRE"]; ?></option>
        <?php
        }
        $db->free_result($qTMP);
        ?>
    </select>
</div>
<div style="width: 100px; height: 30px; float: left;"><b>Grado</b></div>
<div style="width: 900px; height: 30px; float: left;">
    <select name="txtAcademicoCargos_fk-grado">
        <option value=""></option>
        <?php
        $strQuery = "SELECT * FROM cllg_academico_grados WHERE ACTIVO = 'Y'";
        $qTMP = $db->query($strQuery);
        while ($rTMP = $db->fetch_array($qTMP)) {
            $strSelected = ( $strGrado == $rTMP["ID"] ) ? "selected" : "";
        ?>
            <option value="<?php print $rTMP["ID"]; ?>" <?php print $strSelected; ?>><?php print $rTMP["CODIGO"]; ?> - <?php print $rTMP["NOMBRE"]; ?></option>
        <?php
        }
        $db->free_result($qTMP);
        ?>
    </select>
</div>
<div style="width: 100px; height: 30px; float: left;"><b>Activo</b></div>
<div style="width: 900px; height: 30px; float: left;">Si&nbsp;<input type="radio" name="txtAcademicoCargos_activo" value="Y" <?php print ($strActivo == "Y") ? "checked" : ""; ?>>&nbsp;No<input type="radio" name="txtAcademicoCargos_activo" value="N" <?php print ($strActivo == "N") ? "checked" : ""; ?>></div>
<div style="width: 900px; height: 30px; float: left;">&nbsp;</div>
<table width="100%" cellpadding="2" cellspacing="2" border="0" id="tblPadres">
    <tr>
        <td align="left" colspan="6"><b>Cargos Padres de Familia</b></td>
    </tr>
    <tr>
        <td align="center" width="18%" style="background-color: #ECEAE0"><b>Tipo</b></td>
        <td align="center" width="14%" style="background-color: #ECEAE0"><b>Valor</b></td>
        <td align="center" width="10%" style="background-color: #ECEAE0"><b>Vigencia</b></td>
        <td align="center" width="25%" style="background-color: #ECEAE0"><b>Cta. por Cobrar</b></td>
        <td align="center" width="25%" style="background-color: #ECEAE0"><b>Cta. Ingresos</b></td>
        <td align="center" width="8%" style="background-color: #ECEAE0">&nbsp;</td>
    </tr>
    <tr>
        <td align="center"><input type="text" name="txtAcademicoCargosTipoP" value=""></td>
        <td align="center"><input type="text" name="txtAcademicoCargosValorP" value="" style="width: 100px;"></td>
        <td align="center">
            <select name="txtAcademicoCargosVigenciaP">
                <option value="anual">Anual</option>
                <option value="mensual">Mensual</option>
            </select>
        </td>
        <td align="center">
            <input type="text" name="txtAcademicoCargosCCP" value="" style="width: 200px;">
            <input type="hidden" name="hdnAcademicoCargosCCP" value="">
            <script type="text/javascript">
                $(function() {
                    $("[name='txtAcademicoCargosCCP']").autocomplete({
                        
                        source: "admin_academico_cargos_data.php?operation=cuentas",
                        minLength: 1,
                        select: function( event, ui ) {
                            $("input[name='hdnAcademicoCargosCCP']").val(ui.item.id);
                        }
                                
                    });
                });
            </script>
        </td>
        <td align="center">
            <input type="text" name="txtAcademicoCargosCIP" value="" style="width: 200px;">
            <input type="hidden" name="hdnAcademicoCargosCIP" value="">
            <script type="text/javascript">
                $(function() {
                    $("[name='txtAcademicoCargosCIP']").autocomplete({
                        
                        source: "admin_academico_cargos_data.php?operation=cuentas",
                        minLength: 1,
                        select: function( event, ui ) {
                            $("input[name='hdnAcademicoCargosCIP']").val(ui.item.id);
                        }
                                
                    });
                });
            </script>
        </td>
        <td align="center"><button id="btnAgregarPadres" type="button">Agregar</button></td>
    </tr>
    <?php
        $i = 0;
        $strQuery = "SELECT * FROM cllg_academico_cargos_detail WHERE FK_CARGOS = '{$strId}' AND TIPO = 'padres'";
        $qTMP = $db->query($strQuery);
        while ($rTMP = $db->fetch_array($qTMP)) {
            $i++;
    ?>
            <tr>
                <td align="center"><?php print $rTMP["DESCRIPCION"] ?></td>
                <td align="center"><?php print $rTMP["VALOR"] ?></td>
                <td align="center"><?php print $rTMP["VIGENCIA"] ?></td>
                <td align="center"><?php print $rTMP["CTA_COBRAR"] ?></td>
                <td align="center"><?php print $rTMP["CTA_INGRESOS"] ?></td>
                <td align="center">
                    <button type="button" 
                    	onclick="fntAcademicoCargosDelDetalle(<?php print $rTMP["ID"] . "," . $rTMP["FK_CARGOS"] ?>)" name="del">Quitar</button>
                    <input type='hidden' name='hdnAcademicoCargosD_DESCRIPCION_<?php print $i; ?>' value='<?php print $rTMP["DESCRIPCION"] ?>'>
                    <input type='hidden' name='hdnAcademicoCargosD_VALOR_<?php print $i; ?>' value='<?php print $rTMP["VALOR"] ?>'>
                    <input type='hidden' name='hdnAcademicoCargosD_VIGENCIA_<?php print $i; ?>' value='<?php print $rTMP["VIGENCIA"] ?>'>
                    <input type='hidden' name='hdnAcademicoCargosD_CTA-COBRAR_<?php print $i; ?>' value='<?php print $rTMP["CTA_COBRAR"] ?>'>
                    <input type='hidden' name='hdnAcademicoCargosD_CTA-INGRESOS_<?php print $i; ?>' value='<?php print $rTMP["CTA_INGRESOS"] ?>'>
                    <input type='hidden' name='hdnAcademicoCargosD_TIPO_<?php print $i; ?>' value='padres'>
                </td>
            </tr>
    <?php
        }
        $db->free_result($qTMP);
    ?>
        <script type="text/javascript">
            intFilaPadres = <?php print $i; ?>;
        </script>
    </table>
    <br>
    <table width="100%" cellpadding="2" cellspacing="2" border="0" id="tblBenefactores">
        <tr>
            <td align="left" colspan="6"><b>Cargos Benefactores</b></td>
        </tr>
        <tr>
            <td align="center" width="18%" style="background-color: #ECEAE0"><b>Tipo</b></td>
            <td align="center" width="14%" style="background-color: #ECEAE0"><b>Valor</b></td>
            <td align="center" width="10%" style="background-color: #ECEAE0"><b>Vigencia</b></td>
            <td align="center" width="25%" style="background-color: #ECEAE0"><b>Cta. por Cobrar</b></td>
            <td align="center" width="25%" style="background-color: #ECEAE0"><b>Cta. Ingresos</b></td>
            <td align="center" width="8%" style="background-color: #ECEAE0">&nbsp;</td>
        </tr>
        <tr>
            <td align="center"><input type="text" name="txtAcademicoCargosTipoB" value=""></td>
            <td align="center"><input type="text" name="txtAcademicoCargosValorB" value="" style="width: 100px;"></td>
            <td align="center">
                <select name="txtAcademicoCargosVigenciaB">
                    <option value="anual">Anual</option>
                    <option value="mensual">Mensual</option>
                </select>
            </td>
            <td align="center">
                <input type="text" name="txtAcademicoCargosCCB" value="" style="width: 200px;">
                <input type="hidden" name="hdnAcademicoCargosCCB" value="">
                <script type="text/javascript">
                    $(function() {
                        $("[name='txtAcademicoCargosCCB']").autocomplete({

                            source: "admin_academico_cargos_data.php?operation=cuentas",
                            minLength: 1,
                            select: function( event, ui ) {
                                $("input[name='hdnAcademicoCargosCCB']").val(ui.item.id);
                            }

                        });
                    });
                </script>
            </td>
            <td align="center">
                <input type="text" name="txtAcademicoCargosCIB" value="" style="width: 200px;">
                <input type="hidden" name="hdnAcademicoCargosCIB" value="">
                <script type="text/javascript">
                    $(function() {
                        $("[name='txtAcademicoCargosCIB']").autocomplete({

                            source: "admin_academico_cargos_data.php?operation=cuentas",
                            minLength: 1,
                            select: function( event, ui ) {
                                $("input[name='hdnAcademicoCargosCIB']").val(ui.item.id);
                            }

                        });
                    });
                </script>
            </td>
            <td align="center"><button id="btnAgregarBenefactores" type="button">Agregar</button></td>
        </tr>
    <?php
        //$i = 0;
        $strQuery = "SELECT * FROM cllg_academico_cargos_detail WHERE FK_CARGOS = '{$strId}' AND TIPO = 'benefactores'";
        $qTMP = $db->query($strQuery);
        while ($rTMP = $db->fetch_array($qTMP)) {
            $i++;
    ?>
            <tr>
                <td align="center"><?php print $rTMP["DESCRIPCION"] ?></td>
                <td align="center"><?php print $rTMP["VALOR"] ?></td>
                <td align="center"><?php print $rTMP["VIGENCIA"] ?></td>
                <td align="center"><?php print $rTMP["CTA_COBRAR"] ?></td>
                <td align="center"><?php print $rTMP["CTA_INGRESOS"] ?></td>
                <td align="center">
  <button type="button" 
                    	onclick="fntAcademicoCargosDelDetalle(<?php print $rTMP["ID"] . "," . $rTMP["FK_CARGOS"] ?>)" name="del">Quitar</button>
                    &nbsp;
                    <input type='hidden' name='hdnAcademicoCargosD_DESCRIPCION_<?php print $i; ?>' value='<?php print $rTMP["DESCRIPCION"] ?>'>
                    <input type='hidden' name='hdnAcademicoCargosD_VALOR_<?php print $i; ?>' value='<?php print $rTMP["VALOR"] ?>'>
                    <input type='hidden' name='hdnAcademicoCargosD_VIGENCIA_<?php print $i; ?>' value='<?php print $rTMP["VIGENCIA"] ?>'>
                    <input type='hidden' name='hdnAcademicoCargosD_CTA-COBRAR_<?php print $i; ?>' value='<?php print $rTMP["CTA_COBRAR"] ?>'>
                    <input type='hidden' name='hdnAcademicoCargosD_CTA-INGRESOS_<?php print $i; ?>' value='<?php print $rTMP["CTA_INGRESOS"] ?>'>
                    <input type='hidden' name='hdnAcademicoCargosD_TIPO_<?php print $i; ?>' value='benefactores'>
                </td>
            </tr>
    <?php
        }
        $db->free_result($qTMP);
    ?>
        <script type="text/javascript">
            intFilaPadres = <?php print $i; ?>;
        </script>
    </table>
    <script type="text/javascript">
        function fntAcademicoCargosAgregarFilasPadres() {

            var strFila = "";
            var tipo = $("[name='txtAcademicoCargosTipoP']").val();
            var valor = $("[name='txtAcademicoCargosValorP']").val();
            var vigencia = $("[name='txtAcademicoCargosVigenciaP']").val();
            var ccT = $("[name='txtAcademicoCargosCCP']").val();
            var cc = $("[name='hdnAcademicoCargosCCP']").val();
            var ciT = $("[name='txtAcademicoCargosCIP']").val();
            var ci = $("[name='hdnAcademicoCargosCIP']").val();

            intFilaPadres++;

            strFila += "<tr>";
            strFila += "<td align='center'>"+tipo+"</td>";
            strFila += "<td align='center'>"+valor+"</td>";
            strFila += "<td align='center'>"+vigencia+"</td>";
            strFila += "<td align='center'>"+ccT+"</td>";
            strFila += "<td align='center'>"+ciT+"</td>";
            strFila += "<td align='center'>";
            strFila += "<input type='hidden' name='hdnAcademicoCargosD_DESCRIPCION_"+intFilaPadres+"' value='"+tipo+"'>";
            strFila += "<input type='hidden' name='hdnAcademicoCargosD_VALOR_"+intFilaPadres+"' value='"+valor+"'>";
            strFila += "<input type='hidden' name='hdnAcademicoCargosD_VIGENCIA_"+intFilaPadres+"' value='"+vigencia+"'>";
            strFila += "<input type='hidden' name='hdnAcademicoCargosD_CTA-COBRAR_"+intFilaPadres+"' value='"+cc+"'>";
            strFila += "<input type='hidden' name='hdnAcademicoCargosD_CTA-INGRESOS_"+intFilaPadres+"' value='"+ci+"'>";
            strFila += "<input type='hidden' name='hdnAcademicoCargosD_TIPO_"+intFilaPadres+"' value='padres'>";
            strFila += "</td>";
            strFila += "</tr>";
            $("#tblPadres").append(strFila);

            $("[name='txtAcademicoCargosTipoP']").val("");
            $("[name='txtAcademicoCargosValorP']").val("");
            $("[name='txtAcademicoCargosVigenciaP']").val("");
            $("[name='txtAcademicoCargosCCP']").val("");
            $("[name='hdnAcademicoCargosCCP']").val("");
            $("[name='hdnAcademicoCargosCIP']").val("");
            $("[name='txtAcademicoCargosCIP']").val("");

        }
        function fntAcademicoCargosAgregarFilasBenefactores() {

            var strFila = "";
            var tipo = $("[name='txtAcademicoCargosTipoB']").val();
            var valor = $("[name='txtAcademicoCargosValorB']").val();
            var vigencia = $("[name='txtAcademicoCargosVigenciaB']").val();
            var ccT = $("[name='txtAcademicoCargosCCB']").val();
            var cc = $("[name='hdnAcademicoCargosCCB']").val();
            var ciT = $("[name='txtAcademicoCargosCIB']").val();
            var ci = $("[name='hdnAcademicoCargosCIB']").val();

            intFilaPadres++;

            strFila += "<tr>";
            strFila += "<td align='center'>"+tipo+"</td>";
            strFila += "<td align='center'>"+valor+"</td>";
            strFila += "<td align='center'>"+vigencia+"</td>";
            strFila += "<td align='center'>"+ccT+"</td>";
            strFila += "<td align='center'>"+ciT+"</td>";
            strFila += "<td align='center'>";
            strFila += "<input type='hidden' name='hdnAcademicoCargosD_DESCRIPCION_"+intFilaPadres+"' value='"+tipo+"'>";
            strFila += "<input type='hidden' name='hdnAcademicoCargosD_VALOR_"+intFilaPadres+"' value='"+valor+"'>";
            strFila += "<input type='hidden' name='hdnAcademicoCargosD_VIGENCIA_"+intFilaPadres+"' value='"+vigencia+"'>";
            strFila += "<input type='hidden' name='hdnAcademicoCargosD_CTA-COBRAR_"+intFilaPadres+"' value='"+cc+"'>";
            strFila += "<input type='hidden' name='hdnAcademicoCargosD_CTA-INGRESOS_"+intFilaPadres+"' value='"+ci+"'>";
            strFila += "<input type='hidden' name='hdnAcademicoCargosD_TIPO_"+intFilaPadres+"' value='benefactores'>";
            strFila += "</td>";
            strFila += "</tr>";
            $("#tblBenefactores").append(strFila);

            $("[name='txtAcademicoCargosTipoB']").val("");
            $("[name='txtAcademicoCargosValorB']").val("");
            $("[name='txtAcademicoCargosVigenciaB']").val("");
            $("[name='txtAcademicoCargosCCB']").val("");
            $("[name='hdnAcademicoCargosCCB']").val("");
            $("[name='hdnAcademicoCargosCIB']").val("");
            $("[name='txtAcademicoCargosCIB']").val("");

        }
        $(function() {
            $("#btnAgregarPadres").click( function() {
                fntAcademicoCargosAgregarFilasPadres();
            });
            $("#btnAgregarBenefactores").click( function() {
                fntAcademicoCargosAgregarFilasBenefactores();
            });
        });
    </script>
<?php
    }

    function fntAcademicoCargosReporte($db) {

        //VARIBLES
        $strId = "";
        $strQuery = "";
        //VARIBLES

        $strQuery = "SELECT cllg_academico_cargos.* ,
                        cllg_academico_grados.CODIGO CODIGO_G, cllg_academico_grados.NOMBRE AS NOMBRE_G, 
                        cllg_academico_periodos.NOMBRE AS PERIODO
                FROM    cllg_academico_cargos,
                        cllg_academico_grados,
                        cllg_academico_periodos 
                WHERE   cllg_academico_cargos.FK_GRADO = cllg_academico_grados.ID
                AND     cllg_academico_cargos.FK_PERIODO = cllg_academico_periodos.ID  
                ORDER BY CODIGO";
        $qTMP = $db->query($strQuery);
        $intNumRows = $db->num_rows;
?>
        <style type="">
            table {
                border-collapse: collapse;
            }
            table td{
                border: 1px solid #AAAAAA;
            }
            .oddtr {
                background-color:#FFFFFF;
            }
            .eventr {
                background-color:#FFFFFF;
            }
            .trover {
                background-color: #ECEAE0;
            }
            .trclick {
                background-color: #ECEAE0;
            }
        </style>
        <br>
        <br>
        <table width="900" cellpadding="2" cellspacing="1" border="0">
            <tbody>
            <tr>
            <td width="25%" align="center" style="background-color: #ECEAE0;"><b>Codigo</b></td>
            <td width="25%" align="center" style="background-color: #ECEAE0;"><b>Grado</b></td>
            <td width="25%" align="center" style="background-color: #ECEAE0;"><b>Periodo</b></td>
            <td width="25%" style="background-color: #ECEAE0;"><b>Activo</b></td>
             <td width="25%" style="background-color: #ECEAE0;"><strong>Opciones</strong></td></tr>
        </tbody>
        </table>
        <table width="900" cellpadding="2" cellspacing="1" border="0" id="tblAcademicoCargos">
    <?php
        if ($intNumRows) {

            while ($rTMP = $db->fetch_array($qTMP)) {
    ?>
                <tr id="<?php print $rTMP["ID"]; ?>">
                    <td align="left" width="25%"><?php print $rTMP["CODIGO"]; ?></td>
                  <td width="25%" align="center"><?php print $rTMP["CODIGO_G"] . " - " . $rTMP["NOMBRE_G"]; ?></td>
                    <td width="25%" align="center"><?php print $rTMP["PERIODO"]; ?></td>
                    <td width="25%" align="center"><?php print $rTMP["ACTIVO"]; ?></td>
                     <td width="25%" class="button" style="background-color: #ECEAE0;"><button type="button" 
                    	onclick="javascript:fntAcademicoCargosDelCargo(<?php echo $rTMP["ID"] ?>);" name="del">Quitar</button></td>
                </tr>
    <?php
            }
        }
        $db->free_result($qTMP);
    ?>
    </table>
    <script type="text/javascript">
        //these two line adds the color to each different row
        $("#tblAcademicoCargos tr:even").addClass("eventr");;
        $("#tblAcademicoCargos tr:odd").addClass("oddtr");;
        //handle the mouseover , mouseout and click event
        $("#tblAcademicoCargos tr ").mouseover(function() {
            $(this).addClass("trover");
        }).mouseout(function() {
            $(this).removeClass("trover");
        });
		
		$("#tblAcademicoCargos tr td").click(function() {          
			if($(this).attr("class")!="button"){
			   var p=$(this).parent().attr("id");
				$("input[name='hdnAcademicoCargos_Id']").val( p );
            	fntAcademicoCargosContenido();
			}
        });
    </script>
<?php
    }

    function fntAcademicoCargosGrabar($db) {

        $strId = isset($_POST["hdnAcademicoCargos_Id"]) ? $_POST["hdnAcademicoCargos_Id"] : "";
        $strFields = "";
        $strValues = "";

        while ($arrPost = each($_POST)) {
            $arrExplode = explode("_", $arrPost["key"]);

            if ($arrExplode[0] == "txtAcademicoCargos") {
                if ($arrPost["key"] != "hdnAcademicoCargos_Id") {

                    if ($arrExplode[1] == "VIGENCIA-INICIAL" || $arrExplode[1] == "VIGENCIA-FINAL") {
                        $arrTMP = array();
                        $arrTMP = explode("-", $arrPost["value"]);
                        krsort($arrTMP);
                        $arrPost["value"] = implode("-", $arrTMP);
                    }

                    if (empty($strId)) {
                        $strFields .= ( empty($strFields)) ? "" : ",";
                        $strFields .= strtoupper(str_ireplace("-", "_", $arrExplode[1]));

                        $strValues .= empty($strValues) ? "" : ",";
                        $strValues .= "'{$arrPost["value"]}'";
                    } else {
                        $strFields .= ( empty($strFields)) ? "" : ",";
                        $strFields .= strtoupper(str_ireplace("-", "_", $arrExplode[1])) . " = '{$arrPost["value"]}'";
                    }
                }
            }
        }
        if (empty($strId)) {
            $db->query("INSERT INTO cllg_academico_cargos ({$strFields}) VALUES ({$strValues})");
            $intID = mysql_insert_id($db->link_id);
            reset($_POST);
            while ($arrPost1 = each($_POST)) {
                $arrExplode = explode("_", $arrPost1["key"]);
                if ($arrExplode[0] == "hdnAcademicoCargosD" && $arrExplode[1] == "DESCRIPCION") {
                    $strDescripcion = isset($_POST["hdnAcademicoCargosD_DESCRIPCION_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoCargosD_DESCRIPCION_{$arrExplode[2]}"] : "";
                    $strValor = isset($_POST["hdnAcademicoCargosD_VALOR_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoCargosD_VALOR_{$arrExplode[2]}"] : "";
                    $strVigencia = isset($_POST["hdnAcademicoCargosD_VIGENCIA_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoCargosD_VIGENCIA_{$arrExplode[2]}"] : "";
                    $strCTACobrar = isset($_POST["hdnAcademicoCargosD_CTA-COBRAR_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoCargosD_CTA-COBRAR_{$arrExplode[2]}"] : "";
                    $strCTAIngresos = isset($_POST["hdnAcademicoCargosD_CTA-INGRESOS_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoCargosD_CTA-INGRESOS_{$arrExplode[2]}"] : "";
                    $strTipo = isset($_POST["hdnAcademicoCargosD_TIPO_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoCargosD_TIPO_{$arrExplode[2]}"] : "";

                    $db->query("INSERT INTO cllg_academico_cargos_detail (FK_CARGOS, DESCRIPCION, VALOR, VIGENCIA, CTA_COBRAR, CTA_INGRESOS, TIPO)
                            VALUES                                   ('{$intID}', '{$strDescripcion}', '{$strValor}', '{$strVigencia}', '{$strCTACobrar}', '{$strCTAIngresos}', '{$strTipo}' )");
                }
            }
        } else {
            $db->query("UPDATE cllg_academico_cargos SET {$strFields} WHERE ID = '{$strId}'");
            $db->query("DELETE FROM cllg_academico_cargos_detail WHERE FK_CARGOS = '{$strId}'");
            reset($_POST);
            while ($arrPost1 = each($_POST)) {
                $arrExplode = explode("_", $arrPost1["key"]);
                if ($arrExplode[0] == "hdnAcademicoCargosD" && $arrExplode[1] == "DESCRIPCION") {
                    $strDescripcion = isset($_POST["hdnAcademicoCargosD_DESCRIPCION_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoCargosD_DESCRIPCION_{$arrExplode[2]}"] : "";
                    $strValor = isset($_POST["hdnAcademicoCargosD_VALOR_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoCargosD_VALOR_{$arrExplode[2]}"] : "";
                    $strVigencia = isset($_POST["hdnAcademicoCargosD_VIGENCIA_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoCargosD_VIGENCIA_{$arrExplode[2]}"] : "";
                    $strCTACobrar = isset($_POST["hdnAcademicoCargosD_CTA-COBRAR_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoCargosD_CTA-COBRAR_{$arrExplode[2]}"] : "";
                    $strCTAIngresos = isset($_POST["hdnAcademicoCargosD_CTA-INGRESOS_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoCargosD_CTA-INGRESOS_{$arrExplode[2]}"] : "";
                    $strTipo = isset($_POST["hdnAcademicoCargosD_TIPO_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoCargosD_TIPO_{$arrExplode[2]}"] : "";

                    $db->query("INSERT INTO cllg_academico_cargos_detail (FK_CARGOS, DESCRIPCION, VALOR, VIGENCIA, CTA_COBRAR, CTA_INGRESOS, TIPO)
                            VALUES                                   ('{$strId}', '{$strDescripcion}', '{$strValor}', '{$strVigencia}', '{$strCTACobrar}', '{$strCTAIngresos}', '{$strTipo}' )");
                }
            }
        }
    }

    function fntAcademicoCargosContabilidadGetCuentasUltimoNivel($db) {

        $arrResult = array();
        //$empresa = getParam("empresa");
        $empresa = 1;
        $term = getParam("term");
        $strQuery = "SELECT c1.* FROM cllg_contab_cuenta c1
                 LEFT JOIN cllg_contab_cuenta c2 ON(c1.codigo = c2.fk_padre AND c1.fk_empresa = c2.fk_empresa )
                 WHERE (c1.codigo LIKE '%{$term}%' OR c1.nombre LIKE '%{$term}%') 
                 AND    c1.fk_empresa = '{$empresa}'
                 AND    c2.codigo IS NULL";
        $qTMP = $db->query($strQuery);

        if ($rTMP = $db->fetch_array($qTMP)) {

            do {
                $arrTMP = array();
                $arrTMP["id"] = utf8_encode($rTMP["codigo"]);
                $arrTMP["value"] = utf8_encode($rTMP["codigo"] . " - " . $rTMP["nombre"]);

                array_push($arrResult, $arrTMP);
            } while ($rTMP = $db->fetch_array($qTMP));
        }
        $db->free_result($rTMP);

        print json_encode($arrResult);
    }
?>